package com.api.syh56.shengyunhui.controller;

import com.api.syh56.shengyunhui.common.BaseUserInfo;
import com.api.syh56.shengyunhui.common.CommonResult;
import com.api.syh56.shengyunhui.common.Constants;
import com.api.syh56.shengyunhui.common.UserInfoEnu;
import com.api.syh56.shengyunhui.entity.Invoice;
//import com.api.syh56.shengyunhui.service.EmailService;
import com.api.syh56.shengyunhui.entity.dto.InvoiceDTO;
import com.api.syh56.shengyunhui.service.InvoiceService;
import com.api.syh56.shengyunhui.websocket.ChatServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 发票
 */
@RestController
@RequestMapping("invoice")
public class InvoiceController {
    @Autowired
    private InvoiceService iService;

//    @Autowired
//    private EmailService emailService;

//    /**
//     *
//     * @param to   收件邮箱
//     * @param subject   主题
//     * @param content   内容
//     * @return
//     */
//    @PostMapping("email")
//    public CommonResult<Object> sendmail(@RequestParam String to,
//                         @RequestParam String subject,
//                         @RequestParam String content){
//        emailService.sendSimpleMail(to,subject,content);
//        return CommonResult.success(null);
//    }

    @Autowired
    private ChatServer chatServer;


    /**
     * 货主端申请开票
     * @param invoiceDTO
     * @return
     */
    @PostMapping("insert")
    public CommonResult<Object> insert(@RequestBody InvoiceDTO invoiceDTO){
//        iService.insert(invoiceDTO);
        iService.insertNew(invoiceDTO);
        chatServer.onMessage(Constants.webSocket.INVOICE_NEW.toString());
        return CommonResult.success(null);
    }

    @PostMapping("updateById")
    public CommonResult<Object> updateById(@RequestBody Invoice invoice){
        iService.updateById(invoice);
        return CommonResult.success(invoice);
    }

    @PostMapping("deleteById")
    public CommonResult<Object> deleteById(@RequestBody Invoice invoice){
        iService.deleteById(invoice.getId());
        return CommonResult.success(null);
    }

    @PostMapping("selectAll")
    public CommonResult<Object> selectAll(){
        List<Invoice> invoices = iService.selectAll();
        return CommonResult.success(invoices);
    }

    /**
     * 查询发票图片url
     * @param invoice
     * @return
     */
    @PostMapping("selectBy")
    public CommonResult<String> selectBy(@RequestBody Invoice invoice){
        Invoice result = iService.selectBy(invoice.getId());
        String invoiceUrl = result.getInvoiceUrl();
        return CommonResult.success(invoiceUrl);
    }

    /**
     * 查询发票详情
     */
    @PostMapping("findInvoiceDetail")
    public CommonResult<Invoice> findInvoiceDetail(@RequestBody Invoice invoice){
        Invoice result = iService.getById(invoice.getId());
        return CommonResult.success(result);
    }



}
